package org.linlinjava.litemall.db.service;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallUserMapper;
import org.linlinjava.litemall.db.dao.StatMapper;
import org.linlinjava.litemall.db.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class StatService {
    @Resource
    private StatMapper statMapper;
    @Resource
    private LitemallUserMapper userMapper;

    public List<Map> statUser() {
        return statMapper.statUser();
    }

    public List<Map> statOrder() {
        return statMapper.statOrder();
    }

    public List<Map> statGoods() {
        return statMapper.statGoods();
    }

    public List<Map> statIntegral() {
        return statMapper.statIntegral();
    }

    public int queryCount(
        Date start, Date end, Byte type, String nickname, String name, Date payStart, Date payEnd,
        Integer payType, Integer mechanismId, Integer userId,
        Integer page, Integer size, String sort, String order){
        PageHelper.startPage(page, size);
        return (int)statMapper.queryCount(start, end, type, nickname, name, payStart, payEnd, payType, mechanismId, userId);
    }

    public List<LitemallWalletRecord> queryInstitutionalReport(
        Date start, Date end, Byte type, String nickname, String name, Date payStart, Date payEnd,
        Integer page, Integer size, String sort, String order){
        PageHelper.startPage(page, size);
        return statMapper.queryInstitutionalReportMapper(start, end, type, nickname, name, payStart, payEnd);
    }

    public List<LitemallWalletRecordVo> queryInstitutionalReportVo(
            Date start, Date end, Byte type, String nickname, String name, Date payStart, Date payEnd,
            Integer payType, Integer mechanismId, Integer userId,
            Integer page, Integer size, String sort, String order,String phone){
        if(phone!=null && !phone.equals("")){
            LitemallUserExample example = new LitemallUserExample();
            example.or().andMobileEqualTo(phone).andDeletedEqualTo(false);
            LitemallUser user = userMapper.selectOneByExample(example);
            userId = user.getId();
        }
        PageHelper.startPage(page, size);
        return statMapper.queryInstitutionalReportVoMapper(start, end, type, nickname, name, payStart, payEnd, payType, mechanismId, userId);
    }


    public List<LitemallWalletRecord> queryTotalInstitutionalReport(
        Date start, Date end, Byte type, String nickname, String name, Date payStart, Date payEnd,
        String sort, String order){
        return statMapper.queryInstitutionalReportMapper(start, end, type, nickname, name, payStart, payEnd);
    }

    public List<LitemallWalletRecordVo> queryTotalInstitutionalReportVo(
        Date start, Date end, Byte type, String nickname, String name, Date payStart, Date payEnd,
        Integer payType, Integer mechanismId, Integer userId,
        String sort, String order,String phone){
        if(phone!=null && !phone.equals("")){
            LitemallUserExample example = new LitemallUserExample();
            example.or().andMobileEqualTo(phone).andDeletedEqualTo(false);
            LitemallUser user = userMapper.selectOneByExample(example);
            userId = user.getId();
        }
        return statMapper.queryInstitutionalReportVoMapper(start, end, type, nickname, name, payStart, payEnd, payType, mechanismId, userId);
    }
}
